<div id="codearea">
	
	{foreach item=sid from=$forms.site_id}
	<!--<script>{literal}$(function(){add_site({$site_id});});{/literal}</script>-->
	{/foreach}
	
	{if $operation == '更新'}
		{foreach item=site_id from=$forms_staff_auth.site_id}
		<div id="auth_area_{$site_id}" class="authArea">
		<table class="formTable">
		<tbody>
		<tr>
			<th>サイト</th>
			<td>
				<input type="hidden" name="forms_staff_auth[site_id][]" id="site_id" value="{$site_id}" /><span class="float-left">{$site_list[$site_id]}</span><a href="javascript:;" class="float-left btnDel" onClick="del_site({$site_id});">削除</a>
			</td>
		</tr>
		<tr>
			<th>権限</th>
			<td class="authList">
				{if $role[$site_id]|@count > 0}
					<label><input type="radio" name="forms_staff_auth[auth_mode][{$site_id}]" value="role"{if $forms_staff_auth.auth_mode[$site_id]=='role'} checked="checked"{/if} onClick="$('#role_{$site_id}').show();$('#select_{$site_id}').hide();">ロール選択</label>
					<label><input type="radio" name="forms_staff_auth[auth_mode][{$site_id}]" value="auth"{if $forms_staff_auth.auth_mode[$site_id]=='auth'} checked="checked"{/if} onClick="$('#role_{$site_id}').hide();$('#select_{$site_id}').show();">個別選択</label>
					<div id="role_{$site_id}"{if $forms_staff_auth.auth_mode[$site_id]!='role'} style="display:none;"{/if}>
					<select name="forms_staff_auth[role][{$site_id}]">
					{foreach from=$role.$site_id item=r}<option value="{$r.value}" {if $r.selected}selected{/if}>{$r.name}</option>{/foreach}
					</select>
					</div>
				{else}
					<label><input type="radio" name="forms_staff_auth[auth_mode][{$site_id}]" value="role" disabled>ロール選択</label>
					<label><input type="radio" name="forms_staff_auth[auth_mode][{$site_id}]" value="auth" checked>個別選択</label>
				{/if}
				<div id="select_{$site_id}" class="labels" {if $forms_staff_auth.auth_mode[$site_id]!='auth'}style="display:none;"{/if}>
					<ul class="btns b10">
						<li class="btn left c"><a href="javascript:;">全てチェック</a></li>
						<li class="btn right c"><a href="javascript:;">全て外す</a></li>
					</ul>
					{foreach from=$plugin.$site_id item=p}
						<label><input type="checkbox" name="forms_staff_auth[auth][{$site_id}][]" value="{$p.value}" {if $p.checked}checked{/if}><img src="./assets/images/icons/sidemenu/{$p.img_file_name}.png" />{$p.name}</label>
					{/foreach}
				</div>
			</td>
		</tr>
		</tbody>
		</table>
		</div>
		{/foreach}
	{/if}

</div>
<table class="formTable">
<tbody>
<tr>
	<th>サイト追加</th>
	<td class="floats">
		<select id="m_site">{html_options options=$site_list}</select>
		<input type="button" value="追加" onClick="add_site();"/>
	</td>
</tr>
</tbody>
</table>

<script language="javascript">
{if $operation == '更新'}
$(document).ready(function(){
	setRadioAndCheckbox();
	{foreach item=site_id from=$forms_staff_auth.site_id}
	setChecker("#auth_area_{$site_id}");
	{/foreach}	
});
{/if}
var setChecker = function(id)
{
	$('.left a',id).click(function(){
		$('.labels input[type=checkbox]',id).each(function(){
			if(!$(this).attr('checked')){
				$(this).closest("label").click();
			}
		});
	});
	$('.right a',id).click(function(){
		$('.labels input[type=checkbox]',id).each(function(){
			if($(this).attr('checked')){
				$(this).closest("label").click();
			}
		});
	});
}
function add_site(sid)
{
	var id = (sid) ? sid : $("#m_site option:selected").val();
	var f = (id) ? 1 : 0;
	var dup = false;
	var iname = $("#m_site option:selected").text();

	$("#site_id").each(function(){
		if($(this).val()==id){
			dup = true;
			return false;
		}
	});

	if(sid){
		$('#m_site option').each(function(){
			if($(this).val() == sid){
				iname = $(this).text();
			}
		});
	}

	if(dup == false){

		$.ajax({
			url      : "{$smarty.server.SCRIPT_NAME}?op=get_auth&mode=role&site_id=" + id,
			dataType : "json",
			async    : false,
			success  : function( data ){
				
				var html = '';
				html += '<div id="auth_area_'+id+'" class="authArea">';
				html += '<table class="formTable"><tbody>';
				html += '<tr><th>サイト</th><td><input type="hidden" name="forms_staff_auth[site_id][]" id="site_id" value="' + id + '" /><span class="float-left">' + iname + '</span><a href="javascript:;" class="float-left btnDel" onClick="del_site('+id+');">削除</a></td></tr>';
				html += '<tr><th>権限</th>';
				html += '<td class="authList">';
				html += '<div class="authType">';
				if(data.role == ''){
					html += '<label><input type="radio" name="forms_staff_auth[auth_mode]['+id+']" value="role" disabled>ロール選択</label>';
					html += '<label><input type="radio" name="forms_staff_auth[auth_mode]['+id+']" value="auth" checked>個別選択</label>';
					html += '</div>';
					html += '<div id="select_'+id+'" class="labels">';
				}
				else{
					html += '<label><input type="radio" name="forms_staff_auth[auth_mode]['+id+']" value="role" checked onClick="$(\'#role_'+id+'\').show();$(\'#select_'+id+'\').hide();">ロール選択</label>';
					html += '<label><input type="radio" name="forms_staff_auth[auth_mode]['+id+']" value="auth" onClick="$(\'#role_'+id+'\').hide();$(\'#select_'+id+'\').show();">個別選択</label>';
					html += '</div>';
					html += '<div id="role_'+id+'"><select name="forms_staff_auth[role]['+id+']">';
					$.each(data.role, function(){
						html += '<option label="' + this.name + '" value="' + this.value + '">' + this.name + '</option>';
					});
					html += '</select></div>';
					html += '<div id="select_'+id+'" class="labels" style="display:none;">';
				}
				html += '<ul class="btns b10">';
				html += '<li class="btn left c"><a href="javascript:;">全てチェック</a></li>';
				html += '<li class="btn right c"><a href="javascript:;">全て外す</a></li>';
				html += '</ul>';
				$.each(data.plugin, function(){
					html += '<label><input type="checkbox" name="forms_staff_auth[auth]['+id+'][]" value="' + this.value + '"><img src="./assets/images/icons/sidemenu/' + this.name.toLowerCase().split(' ')[0] + '.png" />' + this.name + '</label>\n';
				});
				html += '</div>';
				html += '</td>';
				html += '</tr>';
				html += '</tbody></table>';
				html += '</div>';

				$('#codearea').append(html);
				setRadioAndCheckbox();
				setChecker("#auth_area_"+id);
				$("#auth_area_"+id+" select").uniform();
			},
			error : function(){
			}
		});
	}
}                    
function del_site(sid){
	$("#auth_area_"+sid).remove();
}
</script>
